﻿using System;
using ECommerce.Web.Models;
using FluentNHibernate.Conventions;
using FluentNHibernate.Conventions.AcceptanceCriteria;
using FluentNHibernate.Conventions.Inspections;
using FluentNHibernate.Conventions.Instances;

namespace ECommerce.Web.NHibernate.MappingConventions
{
    public class ReferenceConvention : IReferenceConvention, IReferenceConventionAcceptance
    {
        public void Accept(IAcceptanceCriteria<IManyToOneInspector> criteria)
        {
            criteria.Expect(x => x.EntityType.BaseType == typeof(Entity) && !x.EntityType.IsAbstract);
        }

        public void Apply(IManyToOneInstance instance)
        {
            instance.ForeignKey(String.Format("FK_{0}_{1}", instance.Property.PropertyType.Name, instance.EntityType.Name));
        }
    }
}
